package com.lowkey.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.lowkey.tableInfo.Id;
import com.lowkey.tableInfo.ManyToOne;
import com.lowkey.tableInfo.OneToMany;
import com.lowkey.tableInfo.Property;
import com.lowkey.tableInfo.TableInfo;

public class XMLFactory {
	public static String Path = ""; 
	public static String DataSourcePath = ""; 
	public static Map<String, String> getXMLInfo(String filename){
		Map<String, String> jdbcMap=new HashMap<String, String>();
		File file=new File(filename);
		SAXReader reader=new SAXReader();
		Document doc=null;
		try {
			doc=reader.read(file);
			Element roots = doc.getRootElement();
//			System.out.println(roots.asXML());
			Element root = roots.element("datasource");
			Element jdbc=root.element("jdbc");
			Element dbcpDataSource=root.element("ds");
			if(dbcpDataSource!=null){
				//配置了dbcp的数据源,就不用普通的jdbc连接了
				jdbcMap.put(dbcpDataSource.attributeValue("name"),dbcpDataSource.attributeValue("class"));
				List<Element> propertys =dbcpDataSource.elements();
				for(Element property: propertys){
					Attribute nameAttribute = property.attribute("name");
					Attribute valueAttribute = property.attribute("value");
					if (valueAttribute!=null){//有name  也有value的时候
						jdbcMap.put(nameAttribute.getText(), valueAttribute.getText());
					}else{//有name 也有value的时候
						jdbcMap.put(nameAttribute.getText(), property.getText());
					}
				}
			}else{
				List<Element> propertys= jdbc.elements();
				for(Element property: propertys){
					Attribute nameAttribute = property.attribute("name");
					Attribute valueAttribute = property.attribute("value");
					if (valueAttribute!=null){//有name  也有value的时候
						jdbcMap.put(nameAttribute.getText(), valueAttribute.getText());
					}else{//有name 也有value的时候
						jdbcMap.put(nameAttribute.getText(), property.getText());
					}
				}
			}
//			for(Iterator<Entry<String, String>> it = jdbcMap.entrySet().iterator();it.hasNext();)
//			{
//				Map.Entry<String, String> entry = it.next();
//				System.out.println(entry.getKey()+"----------->"+entry.getValue());
//			}
			Constant.DBMAP=jdbcMap;
			readBeanXML(roots);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		return jdbcMap;
	}
	public static void readBeanXML(Element roots){
		Element orm_mapping = roots.element("orm-mapping");
		Element list = orm_mapping.element("list");
		List<Element> valueList = list.elements();
		List<String> fileList = new ArrayList<String>();
		for(Element e : valueList){
			 fileList.add(Path+e.getText());
		}
		readTableXMLInfo(fileList);
	} 
	public static void readTableXMLInfo (List<String> fileList){
		Constant.TABLEMAP=TableInfoXMLUitls.getTableInfoXML(fileList);
	}
	 
	public static void load(String datasourcePath){
		DataSourcePath=XMLFactory.class.getResource(datasourcePath).getFile().replaceAll("%20", " ");
		Path =DataSourcePath.substring(0,DataSourcePath.lastIndexOf(datasourcePath));
 		getXMLInfo(DataSourcePath);

// 		Map<String,TableInfo> map = Constant.TABLEMAP;
// 		Set<String> set = map.keySet();
// 		Iterator<String> it =set.iterator();
// 		System.out.println("配置文件的信息");
// 		System.out.println("*******************************");
// 		while(it.hasNext()){
// 			String key =it.next();
// 			TableInfo table= map.get(key);
// 			System.out.println("	className:"+table.getClassName()+"	tableName:"+table.getTableName());
// 			Id id = table.getId();
// 			System.out.println("	主键信息: idName:"+ id.getName() +"	idColumn:" + id.getColumn() );
// 			List<Property> plist = table.getPropertys();
// 			System.out.println("	表的列信息:");
// 			for(Property p : plist){
// 				System.out.println("		name:"+p.getName()+"	column:"+p.getColumn());
// 			}
// 			List<OneToMany> olist = table.getOneList();
// 			if(olist.size()>0)System.out.println("	OneToMany的信息:");
// 			for(OneToMany o : olist){
// 				System.out.println("		name:"+o.getOneName()+"	class:" + o.getOneClass()+"	column:"+o.getOneColumn());
// 			}
// 			List<ManyToOne> mlist = table.getManyList();
// 			if(mlist.size()>0)System.out.println("	ManyToOne的信息:");
// 			for(ManyToOne m : mlist){
// 				System.out.println("		name:"+m.getmanyName()+" class:" + m.getmanyClass()+" column:"+m.getmanyColumn());
// 			}
// 			System.out.println("********");
// 		}
	} 
}
